<?php

class userModel extends CI_Model {

	public function getUserData($id){
		$this->db->where('user_id',$id);
		return $this->db->get('user')->result();
	}

	public function getUserAds($id){
		$this->db->where('user.user_id',$id);
		$this->db->join('user','user.user_id = ads.user_id');
		$this->db->join('category','category.cat_id = ads.cat_id');
		return $this->db->get('ads')->result();
	}

	public function searchUsers($tot_num, $offset, $sort_by, $sort_order, $search_q){

	  $this->db->order_by($sort_by, $sort_order);
      
      $this->db->like('full_name', $search_q);
      //tokenize
      $token = strtok($search_q," ");
      while ($token!=false) 
      {
        $this->db->or_like('full_name', $token);
        $this->db->or_like('contact', $token);  
        $token = strtok(" ");
      }

      $query = $this->db->get('user', $tot_num, $offset);
      $ret['records'] = $query->result();


      //for total num rows
      $this->db->order_by($sort_by, $sort_order);
      $this->db->like('full_name', $search_q);
      //tokenize
      $token = strtok($search_q," ");
      while ($token!=false) 
      {
        $this->db->or_like('full_name', $token);
        $this->db->or_like('contact', $token);
        $token = strtok(" ");
      }

      $ret['num_rows'] = $this->db->get('user')->num_rows(); 
      return $ret;
	}


	public function getUserNotes($id){
		$this->db->where('user.user_id',$id);
		$this->db->join('user','user.user_id = notes.user_id');
		return $this->db->get('notes')->result();
	}

}